/**
 * 
 */
package programs;

import java.io.*;

import global.*;
import heap.*;
import diskmgr.*;
import columnar.*;
import bitmap.*;
import btree.*;
import bufmgr.*;
import index.*;

public class index {

	/**
	 * @param args
	 *            args[0] - "index" args[1] - COLUMNDBNAME args[2] -
	 *            COLUMNARFILENAME args[3] - COLUMNNAME args[4] - INDEXTYPE
	 * @throws IOException
	 * @throws ReplacerException
	 * @throws HashEntryNotFoundException
	 * @throws InvalidFrameNumberException
	 * @throws PageUnpinnedException
	 * @throws UnpinPageException
	 * @throws InvalidTupleSizeException
	 * @throws AddFileEntryException
	 * @throws NumberFormatException
	 * @throws BufMgrException 
	 * @throws PageNotFoundException 
	 * @throws PagePinnedException 
	 * @throws HashOperationException 
	 */

	public static void main(String[] args) throws NumberFormatException,
			AddFileEntryException, InvalidTupleSizeException,
			UnpinPageException, PageUnpinnedException,
			InvalidFrameNumberException, HashEntryNotFoundException,
			ReplacerException, IOException, HashOperationException, PagePinnedException, PageNotFoundException, BufMgrException {

		if (args.length != 4) {
			System.out.println("The command line invocation must be as follows: \n\tindex COLUMNDBNAME COLUMNARFILENAME COLUMNNAME INDEXTYPE");
			Runtime.getRuntime().exit(1);
		}

		String _columnDBName;
		String _columnFileName;
		String _columnName;
		int _indexType = -1;
		Columnarfile _cf = null;
		IndexFile _if = null;

		_columnDBName = args[0];
		_columnFileName = args[1];
		_columnName = args[2];

		if (args[3].equals("BTREE"))
			_indexType = IndexType.B_Index;
		else if (args[3].equals("BITMAP"))
			_indexType = IndexType.BitMapIndex;
		else {
			System.out.println("Only two types of indexes are allowed: BTREE or BITMAP");
			Runtime.getRuntime().exit(1);
		}

		// read an existing DB into BufferManager
		SystemDefs sysdef = new SystemDefs(_columnDBName, 0, 50, "Clock");

		// try read a columnarfile form DB
		try {
		 	_cf = new Columnarfile(_columnFileName);
		} catch (Exception ex) {
			System.out.println("Unable to read a columnar file from DB\n\t check if it has been created first\n");
			Runtime.getRuntime().exit(1);
		}

		// initialize an index file
		switch (_indexType) {
			case IndexType.BitMapIndex:
				try {
					System.out.println("Please input the value for bitmap index:");
					InputStreamReader inp = new InputStreamReader(System.in);
					BufferedReader br = new BufferedReader(inp);
					ValueClass _value = new StringValue(br.readLine());
					br.close();
					_cf.createBitMapIndex(_cf.getColumnOffset(_columnName)-1, _value);
					
				} 
				catch (Exception e) {
					System.err.println(e.toString());
					Runtime.getRuntime().exit(1);
				}
				break;
	
			case IndexType.B_Index:
				try {
					_cf.createBTreeIndex(_columnName);				
				} 
				catch (Exception e) {
					System.err.println(e.toString());
					Runtime.getRuntime().exit(1);
				} 
				break;
			default:
				break;
		}
		
		System.out.println(args[3]+ " index on column " + _columnName + " is created successfuly");

        sysdef.JavabaseBM.flushAllPages();

        PCounter.print();
        Runtime.getRuntime().exit(0);
	}

}
